const formatTime = (date, text) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  const week = date.getDay()
  return {
    yyyy: formatNumber(year),
    month: formatNumber(month),
    dd: formatNumber(day),
    week: formatWeek(week, text),
    mm_dd: formatNumber(month) + '月' + formatNumber(day) + '日',
    yyyy_mm_dd: [year, month, day].map(formatNumber).join('-'),
    hh_mm: [hour, minute].map(formatNumber).join(':'),
    yyyy_mm_dd_hh_mm_ss: [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':'),
    mm_dd_week: formatNumber(month) + '月' + formatNumber(day) + '日' + ' ' + formatWeek(week, text)
  }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatWeek = (n, t) => {
  let text = t ? t : '星期'
  let x = ''
  switch (n) {
    case 0:
      x = text + '日';
      break;
    case 1:
      x = text + '一';
      break;
    case 2:
      x = text + '二';
      break;
    case 3:
      x = text + '三';
      break;
    case 4:
      x = text + '四';
      break;
    case 5:
      x = text + '五';
      break;
    case 6:
      x = text + '六';
      break;
  }
  return x
}

module.exports = {
  formatTime: formatTime,
  formatNumber: formatNumber,
  formatWeek: formatWeek
}